<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户账号" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户账号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择性别" clearable size="small">
          <el-option
            v-for="dict in sexOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['basicInformation:appUserY:add']"
        >新增
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['basicInformation:appUserY:edit']"-->
      <!--        >修改-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['basicInformation:appUser:import']"
        >导入
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['basicInformation:appUserY:remove']"
        >批量删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleLogin"
          v-hasPermi="['basicInformation:appUserY:add']"
        >报批登记
        </el-button>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="setPrint"
            v-hasPermi="['frontierEvent:appUserY:export']"
          >打印
          </el-button>
        </el-col>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click=""-->
      <!--          v-hasPermi="['basicInformation:appUserY:add']"-->
      <!--        >船员任职记录登记</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['basicInformation:appUserY:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="appUserList" @selection-change="handleSelectionChange" id="tableList">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主建id" align="center" prop="id" v-if="false"/>
      <el-table-column label="在线状态" align="center" prop="isOnline">
        <template slot-scope="scope">
          <el-tooltip placement="top" content="在线" effect="dark" v-if="scope.row.isOnline === false">
            <img src="../../../../public/image/car/green-lamp.png"/>
          </el-tooltip>
          <el-tooltip placement="top" content="离线" effect="dark" v-if="scope.row.isOnline === true">
            <img src="../../../../public/image/car/red-lamp.png"/>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="定位" align="center" prop="isOnline">
        <template slot-scope="scope">
          <el-button v-if="scope.row.isOnline !== null"
                     size="mini"
                     type="text"
                     @click="handleOpenMap(scope.row,1)"
          >定位
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="终端名称" align="center" prop="equipmentName"/>
      <el-table-column label="终端编号" align="center" prop="equipmentNumber"/>
      <el-table-column label="船舶编码" align="center" prop="shipCode"/>
      <el-table-column label="用户账号" align="center" prop="userName"/>
      <el-table-column label="姓名" align="center" prop="name"/>
      <el-table-column label="身份证号码" align="center" prop="idcard"/>
      <el-table-column label="性别" align="center" prop="sex" :formatter="sexFormat"/>
      <!--      <el-table-column label="身体状况" align="center" prop="physical"/>-->
      <el-table-column label="联系方式" align="center" prop="phonenumber"/>
      <el-table-column label="职位" align="center" prop="position" :formatter="positionFormat"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['basicInformation:appUserY:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['basicInformation:appUserY:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-key"
            @click="handleResetPwd(scope.row)"
            v-hasPermi="['system:appUser:resetPwd']"
          >重置
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改APP用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row v-if="title === '修改渔民信息'">
        </el-row>
        <el-row v-else>
          <el-col :span="12">
            <el-form-item label="用户账号" prop="userName">
              <el-input v-model="form.userName" placeholder="请输入用户账号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="form.userId == undefined" label="用户密码" prop="password">
              <el-input v-model="form.password" placeholder="请输入用户密码" type="password"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入姓名"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="归属部门" prop="deptId">
              <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属部门"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="角色">
              <el-select v-model="form.roleIds" multiple placeholder="请选择">
                <el-option
                  v-for="item in roleOptions"
                  :key="item.roleId"
                  :label="item.roleName"
                  :value="item.roleId"
                  :disabled="item.status == 1"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="sex">
              <el-select v-model="form.sex" placeholder="请选择性别">
                <el-option
                  v-for="dict in sexOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="身份证号码" prop="idcard">
              <el-input v-model="form.idcard" placeholder="请输入身份证号码"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系方式" prop="phonenumber">
              <el-input v-model="form.phonenumber" placeholder="请输入联系方式"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="身体状况" prop="physical">
              <el-input v-model="form.physical" placeholder="请输入身体状况"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="船舶编码" prop="manufactor">
              <el-input v-model="form.shipCode" readonly style="width:122px"/>
              <el-button type="primary" v-model="form.shipId" @click="selectShip">选择船只</el-button>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="职位" prop="position">
              <el-select v-model="form.position" placeholder="请选择职位">
                <el-option
                  v-for="dict in positionOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="终端设备名称" prop="equipmentName">
              <el-input v-model="form.equipmentName" readonly/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="终端设备编码" prop="equipmentNumber">
              <el-input v-model="form.equipmentNumber" readonly style="width:222px"/>
              <el-button type="primary" v-model="form.terminalId" @click="selectTerminal">选择终端设备</el-button>
              <el-button type="primary" @click="unBundling" v-show="form.terminalId !== undefined">解绑
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属区域" prop="parentId">
              <treeselect v-model="form.areaId" :options="districtOptions" :show-count="true" placeholder="请选择区域"/>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport + '&userType=1'"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-checkbox v-model="upload.updateSupport"/>
          是否更新已经存在的用户数据
          <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="titleShip" :visible.sync="openShip" width="800px" append-to-body>
      <el-form :model="queryShipParams" ref="queryShipForm" :inline="true" v-show="showSearchShip" label-width="68px">
        <el-form-item label="船名" prop="shipName">
          <el-input
            v-model="queryShipParams.shipName"
            placeholder="请输入船名"
            clearable
            size="small"
            @keyup.enter.native="handleQueryShip"
          />
        </el-form-item>
        <el-form-item label="船舶编码" prop="shipCode">
          <el-input
            v-model="queryShipParams.shipCode"
            placeholder="请输入船舶编码"
            clearable
            size="small"
            @keyup.enter.native="handleQueryShip"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryShip">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetShipQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="shipList" @selection-change="handleSelectionChangeShip">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="主建id" align="center" prop="id" v-if="false"/>
        <el-table-column label="船名" align="center" prop="shipName"/>
        <el-table-column label="船舶编码" align="center" prop="shipCode"/>
      </el-table>
      <pagination
        v-show="totalShip>0"
        :total="totalShip"
        :page.sync="queryShipParams.pageNum"
        :limit.sync="queryShipParams.pageSize"
        @pagination="getShipList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="okShip">确 定</el-button>
        <el-button @click="cancelShip">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="titleTerminal" :visible.sync="openTerminal" width="800px" append-to-body>
      <el-form :model="queryTerminalParams" ref="queryTerminalForm" :inline="true" v-show="showSearchTerminal"
               label-width="68px">
        <el-form-item label="设备名称" prop="equipmentName">
          <el-input
            v-model="queryTerminalParams.equipmentName"
            placeholder="请输入设备名称"
            clearable
            size="small"
            @keyup.enter.native="handleQueryTerminal"
          />
        </el-form-item>
        <el-form-item label="设备编号" prop="equipmentNumber">
          <el-input
            v-model="queryTerminalParams.equipmentNumber"
            placeholder="请输入设备编号"
            clearable
            size="small"
            @keyup.enter.native="handleQueryTerminal"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryTerminal">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetTerminalQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="terminalList" @selection-change="handleSelectionChangeTerminal">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="主建id" align="center" prop="id" v-if="false"/>
        <el-table-column label="设备名称" align="center" prop="equipmentName"/>
        <el-table-column label="设备编号" align="center" prop="equipmentNumber"/>
      </el-table>
      <pagination
        v-show="totalTerminal>0"
        :total="totalTerminal"
        :page.sync="queryTerminalParams.pageNum"
        :limit.sync="queryTerminalParams.pageSize"
        @pagination="getTerminalList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="okTerminal">确 定</el-button>
        <el-button @click="cancelTerminal">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="'地图'"
               :fullscreen="true"
               :visible="openMap"
               :close-on-click-modal="false"
               class="dialog_collect"
               @open="handleOpenDialog"
               @close="handleCloseDialog">
      <section class="dialog_body">
        <common-map
          ref="map"
          style="height: calc(100vh - 110px)"
          @map-click="ShowDetail"
          :isTool="false"
          :toolbarSelectVisible="false"
        >
        </common-map>
      </section>
      <user-detail
        :visible='showUserDialog'
        @close="showDetailDialog = -1"
        ref="userDetail">
      </user-detail>
      <section slot="footer" class="dialog-footer">
        <el-button @click="handleCloseDialog" type="danger">关 闭</el-button>
      </section>
    </el-dialog>
  </div>
</template>

<script>
  import {
    unBundling,
    listAppUser,
    getAppUser,
    delAppUser,
    addAppUser,
    updateAppUser,
    exportAppUser,
    importTemplate
  } from '@/api/basicInformation/appUserY'
  import {resetUserPwd, getUser} from '@/api/system/user'
  import {getToken} from '@/utils/auth'
  import {treeselect} from '@/api/system/dept'
  import {treeselectArea} from '@/api/basicInformation/area'
  import Treeselect from '@riophae/vue-treeselect'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'
  import {listShip} from '@/api/basicInformation/ship'
  import {listTerminal} from '@/api/basicInformation/terminal'

  export default {
    name: 'AppUserY',
    components: {
      Treeselect
    },
    computed: {
      showUserDialog() {
        return this.showDetailDialog === 1
      }
    },
    data() {
      return {
        idsTerminal: [],
        terminalNames: [],
        terminalNumbers: [],
        // 显示搜索条件
        showSearchTerminal: true,
        totalTerminal: 0,
        terminalList: [],
        titleTerminal: '选择终端设备',
        openTerminal: false,
        queryTerminalParams: {
          pageNum: 1,
          pageSize: 10,
          equipmentName: undefined,
          equipmentNumber: undefined,
          terminalType: '1',
          isBinding: '1'
        },
        userId: 0,
        idsShip: [],
        shipCodes: [],
        // 显示搜索条件
        showSearchShip: true,
        totalShip: 0,
        shipList: [],
        titleShip: '选择船只',
        openShip: false,
        queryShipParams: {
          pageNum: 1,
          pageSize: 10,
          shipName: undefined,
          shipCode: undefined
        },
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        names: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // APP用户表格数据
        appUserList: [],
        // 弹出层标题
        title: '',
        // 部门树选项
        deptOptions: undefined,
        // 是否显示弹出层
        open: false,
        // 性别字典
        sexOptions: [],
        // 区域树选项
        districtOptions: undefined,
        // 职位字典
        positionOptions: [],
        // 角色选项
        roleOptions: [],
        // 岗位选项
        postOptions: [],
        // 用户类型字典
        userTypeOptions: [],
        // 用户导入参数
        upload: {
          // 是否显示弹出层（用户导入）
          open: false,
          // 弹出层标题（用户导入）
          title: '',
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          updateSupport: 0,
          // 设置上传的请求头部
          headers: {Authorization: 'Bearer ' + getToken()},
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + '/basicInformation/appUser/importData'
        },
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          loginName: undefined,
          name: undefined,
          sex: undefined,
          userType: 1,
          orderByColumn: 'createTime',
          isAsc: 'desc'
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          userName: [
            {required: true, message: '用户账号不能为空', trigger: 'blur'},
            {
              pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{4,16}$/,
              message: "由数字和字母组成,且长度要在4-16位之间",
              trigger: "blur"
            }
          ],
          name: [
            {required: true, message: '姓名不能为空', trigger: 'blur'},
            {min: 0, max: 30, message: '长度在 0 到 30 个字符', trigger: 'blur'}
          ],
          phonenumber: [
            {required: true, message: '联系方式不能为空', trigger: 'blur'},
            {
              pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
              message: '请输入正确的手机号码',
              trigger: 'blur'
            }
          ],
          idcard: [
            {required: true, message: '用户身份证不能为空', trigger: 'blur'},
            {
              pattern: /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
              message: '请输入正确格式的身份证',
              trigger: 'blur'
            }
          ],
          password: [
            {required: true, message: '用户密码不能为空', trigger: 'blur'},
            {
              // pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/,
              pattern: /^[0-9]{8,16}$/,
              message: "由数字组成,且长度要在8-16位之间",
              trigger: "blur"
            }
          ],
          equipmentNumber: [
            {required: true, message: '请绑定终端', trigger: 'change'}
          ],
        },
        showDetailDialog: -1,
        openMap: false,
        marker: [],
      }
    },
    created() {
      this.getList()
      this.getTreeselect()
      this.getAreaTreeselect();
      this.getDicts('sys_user_sex').then(response => {
        this.sexOptions = response.data
      })
      this.getDicts('app_post').then(response => {
        this.positionOptions = response.data
      })
      this.getDicts('user_type').then(response => {
        this.userTypeOptions = response.data
      })
    },
    methods: {
      getAreaTreeselect() {
        treeselectArea().then(response => {
          this.districtOptions = response.data
        })
      },
      setPrint() {
        // var html = document.getElementById("tableList").innerHTML;
        var w = window.open(location.href)
        // var style = w.document.createElement("style");
        // style.type = "text/css";
        // style.innerText = `打印内容元素的样式...`;
        var tableStr = '<table style=\'width:100%;\'><tr><td style=\'text-align: center;\'>用户账号</td><td style=\'text-align: center;\'>姓名</td><td style=\'text-align: center;\'>身份证号码</td><td style=\'text-align: center;\'>性别</td><td style=\'text-align: center;\'>身体状况</td><td style=\'text-align: center;\'>联系方式</td><td style=\'text-align: center;\'>职位</td></tr>'
        for (var i = 0; i < this.appUserList.length; i++) {
          var sexName = this.selectDictLabel(this.sexOptions, this.appUserList[i].sex)
          var name = this.appUserList[i].name === null ? '' : this.appUserList[i].name
          var idcard = this.appUserList[i].idcard === null ? '' : this.appUserList[i].idcard
          var phonenumber = this.appUserList[i].phonenumber === null ? '' : this.appUserList[i].phonenumber
          var position = this.appUserList[i].position === null ? '' : this.appUserList[i].position
          var physical = this.appUserList[i].physical === null ? '' : this.appUserList[i].physical
          tableStr += '<tr><td style=\'text-align: center;\'>' + this.appUserList[i].userName + '</td><td style=\'text-align: center;\'>' + name + '</td><td style=\'text-align: center;\'>' + idcard + '</td><td style=\'text-align: center;\'>' + sexName + '</td><td style=\'text-align: center;\'>' + physical + '</td><td style=\'text-align: center;\'>' + phonenumber + '</td><td style=\'text-align: center;\'>' + position + '</td></tr>'
        }
        tableStr += '</table>'
        w.document.body.innerHTML = tableStr
        w.document.close()
        w.print()
        w.close()
      },
      getTerminalList() {
        this.loading = true
        listTerminal(this.queryTerminalParams).then(response => {
          this.terminalList = response.rows
          this.totalTerminal = response.total
          this.loading = false
        })
      },
      okTerminal() {
        if (this.idsTerminal.length > 1) {
          this.msgInfo('只能绑定一个终端设备')
        } else if (this.idsTerminal.length === 1) {
          this.form.terminalId = this.idsTerminal[0]
          this.form.equipmentName = this.terminalNames[0]
          this.form.equipmentNumber = this.terminalNumbers[0]
          this.openTerminal = false
        } else {
          this.msgInfo('请选择终端设备')
        }
      },
      cancelTerminal() {
        this.openTerminal = false
      },
      handleQueryTerminal() {
        this.queryTerminalParams.pageNum = 1
        this.getTerminalList()
      },
      resetTerminalQuery() {
        this.resetForm('queryTerminalForm')
        this.handleQueryTerminal()
      },
      // 多选框选中数据
      handleSelectionChangeTerminal(selection) {
        this.idsTerminal = selection.map(item => item.id)
        this.terminalNames = selection.map(item => item.equipmentName)
        this.terminalNumbers = selection.map(item => item.equipmentNumber)
      },
      selectTerminal() {
        this.openTerminal = true
        this.handleQueryTerminal()
      },
      getShipList() {
        this.loading = true
        listShip(this.queryShipParams).then(response => {
          this.shipList = response.rows
          this.totalShip = response.total
          this.loading = false
        })
      },
      okShip() {
        if (this.idsShip.length > 1) {
          this.msgInfo('只能绑定一条船只')
        } else if (this.idsShip.length === 1) {
          this.form.shipId = this.idsShip[0]
          this.form.shipCode = this.shipCodes[0]
          this.openShip = false
        } else {
          this.msgInfo('请选择船只')
        }
      },
      cancelShip() {
        this.openShip = false
      },
      handleQueryShip() {
        this.queryShipParams.pageNum = 1
        this.getShipList()
      },
      resetShipQuery() {
        this.resetForm('queryShipForm')
        this.handleQueryShip()
      },
      // 多选框选中数据
      handleSelectionChangeShip(selection) {
        this.idsShip = selection.map(item => item.id)
        this.shipCodes = selection.map(item => item.shipCode)
      },
      selectShip() {
        this.openShip = true
        this.handleQueryShip()
      },
      /** 查询APP用户列表 */
      getList() {
        this.loading = true
        listAppUser(this.queryParams).then(response => {
          this.appUserList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      unBundling() {
        let _thisSelf = this
        this.$confirm('是否确认解绑终端设备', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          const postData = {
            terminalId: _thisSelf.form.terminalId
          }
          unBundling(postData).then(rs => {
            return rs.code === 200
          })

        }).then(() => {
          this.msgSuccess('解绑成功')
          this.open = false
          this.getList()
        })
      },
      /** 查询部门下拉树结构 */
      getTreeselect() {
        treeselect().then(response => {
          this.deptOptions = response.data
        })
      },
      // 性别字典翻译
      sexFormat(row, column) {
        return this.selectDictLabel(this.sexOptions, row.sex)
      },
      // 职位字典翻译
      positionFormat(row, column) {
        return this.selectDictLabel(this.positionOptions, row.position)
      },
      // 用户类型字典翻译
      userTypeFormat(row, column) {
        return this.selectDictLabel(this.userTypeOptions, row.userType)
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          userName: undefined,
          nickName: undefined,
          name: undefined,
          password: undefined,
          // 默认密码
          initPassword: undefined,
          idcard: undefined,
          terminalId: undefined,
          terminalIdOld: undefined,
          equipmentNumber: undefined,
          equipmentName: undefined,
          sex: undefined,
          physical: undefined,
          contact: undefined,
          position: undefined,
          deptId: undefined,
          userType: 1,
          shipId: undefined,
          delFlag: undefined,
          createById: undefined,
          createBy: undefined,
          createTime: undefined,
          updateById: undefined,
          updateBy: undefined,
          updateTime: undefined,
          roleIds: []
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.userId)
        this.names = selection.map(item => item.name)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.getTreeselect()
        getAppUser(this.userId, 1).then(response => {
          this.roleOptions = response.roles
          this.form.roleIds = response.roleIds
        })
        this.open = true
        this.title = '添加渔民信息'
        this.form.password = this.initPassword
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const userId = row.userId || this.ids
        getAppUser(userId, 1).then(response => {
          this.form = response.data
          this.form.terminalIdOld = this.form.terminalId
          this.form.roleIds = response.roleIds
          this.roleOptions = response.roles
          this.open = true
          this.title = '修改渔民信息'
          this.form.password = ''
        })
        // getAppUser(id).then(response => {
        //   this.form = response.data;
        //   this.form.roleIds = response.roleIds;
        //   this.open = true;
        //   this.title = "修改用户信息";
        //   this.form.password = "";
        // });
      },
      /** 重置密码按钮操作 */
      handleResetPwd(row) {
        this.$prompt('请输入"' + row.userName + '"的新密码', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }).then(({value}) => {
          resetUserPwd(row.userId, value).then(response => {
            this.msgSuccess('修改成功，新密码是：' + value)
          })
        }).catch(() => {
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.userId != null) {
              updateAppUser(this.form).then(response => {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              this.form.isPC = 0
              addAppUser(this.form).then(response => {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      handleLogin() {
        this.msgSuccess('此功能为预留项')
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.userId || this.ids
        const names = row.name || this.names
        this.$confirm('是否确认删除姓名为"' + names + '"的数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          return delAppUser(ids)
        }).then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
      },
      /** 导出按钮操作 */
      handleExport(row) {
        const queryParams = this.queryParams
        queryParams.ids = row.userId || this.ids
        this.$confirm(queryParams.ids.length > 0 ? '是否确认导出所选数据项' : '是否确认导出所有APP用户数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          return exportAppUser(queryParams)
        }).then(response => {
          this.download(response.msg)
        })
      },
      /** 导入按钮操作 */
      handleImport() {
        this.upload.title = '渔民导入'
        this.upload.open = true
      },
      /** 下载模板操作 */
      importTemplate() {
        importTemplate().then(response => {
          this.download(response.msg)
        })
      },
      // 文件上传中处理
      handleFileUploadProgress(event, file, fileList) {
        this.upload.isUploading = true
      },
      // 文件上传成功处理
      handleFileSuccess(response, file, fileList) {
        this.upload.open = false
        this.upload.isUploading = false
        this.$refs.upload.clearFiles()
        this.$alert(response.msg, '导入结果', {dangerouslyUseHTMLString: true})
        this.getList()
      },
      // 提交上传文件
      submitFileForm() {
        this.$refs.upload.submit()
      },
      handleOpenMap(row, e) {
        this.openMap = true
        this.rowData = row
      },
      handleOpenDialog() {
        this.$nextTick(() => {
          this.map = this.$refs.map.map
          this.marker.push({
            coordinates: [this.rowData.x, this.rowData.y],
            id: this.rowData.id,
            title: this.rowData.shipName,
            target: 'fisher',
            animate: true,
            stopEvent: true,
            iconUrl: '/image/map/fisher.png',
            item: this.rowData
          })
          this.$refs.map.showMarkerFeature(this.marker, false, 'fisher')
          this.$refs.map.map.getMap().getView().animate({
            center: [this.rowData.x, this.rowData.y],
            duration: 500
          })
        })
      },
      ShowDetail(e) {
        this.$refs.boatDetail.handleShowCaseInfo(e, this.map)
        this.showDetailDialog = 2
      },
      handleCloseDialog() {
        this.showDetailDialog = -1
        this.marker = []
        this.openMap = false
        this.$forceUpdate()
      }
    }
  }
</script>

